﻿using Aspose.Cells;
using CJAtech.BBS.Business;
using CJAtech.BBS.DataEntity;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using TjZheng.Base;
using TjZheng.CMS.Process;
using TjZheng.Controls;
using TjZheng.Framework;
using TjZheng.Logging;

namespace TjZheng.CMS.Areas.BBS.Controllers
{
    public partial class ArticleInfoController
    {
        //
        // GET: /BBS/ArticleInfoController.Business/

        [QueryMsSqlFilter]
        public ActionResult BenzExport()
        {
            WorkbookDesigner objWorkbookDesigner = new WorkbookDesigner();
            objWorkbookDesigner.Open(Server.MapPath("~/Areas/BBS/Views/ArticleInfo/BenzArticleInfoList.xls"));

            BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo(objDbSetInfo.ConnectionString, objDbSetInfo.TableExtension);
            CategoryInfo objCategoryInfo = objBizCategoryInfo.GetRecord(GetInt("CategoryId"));
            objBizArticleInfo = new BizArticleInfo(objDbSetInfo.ConnectionString, objDbSetInfo.TableExtension);
            //todo：帖子量少，量多的操作情况
            var objDataTable = objBizArticleInfo.GetDataTable("IsRelease=1 AND CategoryIDPath like @CategoryIDPath", "CategoryIDPath".CreateSqlParameters(objCategoryInfo.IDPath.FormatSelf("{0}%")), "ArticleId asc");
            objDataTable.TableName = "ArticleInfo";
            objDataTable.Columns.Add("IsReleaseString", typeof(string));
            foreach (DataRow objRow in objDataTable.Rows)
            {
                objRow["IsReleaseString"] = objRow["IsRelease"].ToString() == "0" ? "未发布" : "已发布";
            }
            objWorkbookDesigner.SetDataSource(objDataTable);
            objWorkbookDesigner.Process();
            string fileName = Server.MapPath("~/Docs/品牌故事.xls");
            //objWorkbookDesigner.Save(fileName, SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
            fileName.CreateDirectory();
            objWorkbookDesigner.Save(fileName, FileFormatType.Excel2003);
            return File(fileName, "application/ms-excel", "品牌故事.xls");
        }

        public ActionResult BenzEditUser(int id)
        {
            objBizArticleInfo = new BizArticleInfo(objDbSetInfo.ConnectionString, objDbSetInfo.TableExtension);
            var objArticleInfo = objBizArticleInfo.GetRecord(id);
            if (objArticleInfo == null)
            {
                return Content("对不起，文章不存在或已被删除");
            }
            return View(objArticleInfo);
        }

        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult BenzUserJson(int page, int rows, string param)
        {
            ///bbs/articleinfo/benzuserjson?setid=2&page=1&rows=10&param=徐丁一
            string url = "http://bmbshrnew.fpowerglobal.com/api/common?API=UserInfoList&page=" + page + "&pagesize=" + rows + "&param=" + param;
            var objHttpWebRequest = url.CreateHttpRequest(null, "", null, "text/html, application/xhtml+xml, */*");
            objHttpWebRequest.Method = "Get";
            var objDict = objHttpWebRequest.GetHttpResponse(Encoding.UTF8).JsonDeserialize<Dictionary<string, object>>();
            object obj = null;
            PageResult<object> objPageResult = new PageResult<object>
            {
                pageIndex = page,
                pageSize = rows
            };
            if (!objDict.TryGetValue("ApiParamObj", out obj))
            {
                LogHelper.WriteLog("获取奔驰用户接口结果未找到ApiParamObj属性", objDict.JsonSerialize());
                return Json(objPageResult, JsonRequestBehavior.AllowGet);
            }
            var objResult = obj as ArrayList;
            var objUsers = objResult[0] as ArrayList;
            var objCounts = objResult[1] as ArrayList;
            var objDictCount = objCounts[0] as Dictionary<string, object>;
            objPageResult.rows = objUsers.ToArray().ToList();
            objPageResult.total = (int)objDictCount["total"];
            return Json(objPageResult, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SaveUser(int id, string objectId, string articleAuthor, string s1)
        {
            InvokeResult objInvokeResult = new InvokeResult();
            if (objectId.IsNull() || articleAuthor.IsNull())
            {
                objInvokeResult.ResultCode = "ParamsEmpty";
                objInvokeResult.ResultMessage = "objectId或者articleAuthor不能为空";
                return Json(objInvokeResult, "text/plain");
            }
            objBizArticleInfo = new BizArticleInfo(objDbSetInfo.ConnectionString, objDbSetInfo.TableExtension);
            if (!objBizArticleInfo.Any("ArticleId=" + id))
            {
                objInvokeResult.ResultCode = "ArticleEmpty";
                objInvokeResult.ResultMessage = "对不起，文章不存在";
                return Json(objInvokeResult, "text/plain");
            }
            if (s1.IsNoNull())
            {
                if (s1.IndexOf("http://") < 0)
                {
                    s1 = "http://bmbsuc.fpowerglobal.com/avatar/" + s1;
                }
            }
            s1 = s1 ?? "";
            objBizArticleInfo.Update("ObjectId=@ObjectId,ArticleAuthor=@ArticleAuthor,S1=@S1", id, "ObjectId,ArticleAuthor,S1".CreateSqlParameters(objectId, articleAuthor, s1));
            objInvokeResult.ResultMessage = "保存成功";
            return Json(objInvokeResult, "text/plain");
        }
    }
}
